<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  users
 */

class m_users_forms_checkers_checkUserMail extends f_form_Checker
{
	public function check()
	{		
		$documentProvider = f_document_Provider::getInstance();
        $mode  = $this->getAttribute("mode");

        if(!is_null($this->getFormValue("id")))
        {
            $group = f_document_Provider::getInstance()->getByModelAndUniqueId('users/User', $this->getFormValue("id"))->getDocumentParent();
        }
        else
        {
            if(!is_null($this->getFormValue('group')))
            {
                $group = f_document_Provider::getInstance()->getByModelAndUniqueId('users/Group', $this->getFormValue('group'));
            }
        }

        $id = !is_null($this->getFormValue("id")) ? $this->getFormValue("id") : '';

		$data = $documentProvider
						->loadDocuments("modules_users/user")
						->whereIsEqual("email", $this->getValue())
						->whereIsNotEqual("document_uid", $id);
        /*
        if($group != null)
        {
            $data->isChildOf($group->getUniqueId());
        }
         */
        
        $nb = $data->count();

		if($nb > 0 && $mode == "exist")
		{
			return true;
		}
        elseif($nb == 0 && $mode == "notExist")
        {
            return true;
        }
		
		return $this->getErrorMessage();
	}
}